<?php if (empty($this->data)) : // ถ้าไม่มีข้อมูล ?>
<div class="success-flash">No Alert Product.</div>
<? else : // ถ้ามีข้อมูล ?>
    <?php $paginator->options(array('update' => 'divCustInventories', 'indicator' => 'indicator')); ?>
    <?php echo $this->element('dataCaptionLimit', array('update' => 'divCustInventories', 'action' => 'cust_inventories')); ?>
    <?php $cur = $paginator->counter(array('format' => '%start%')); ?>
<table>
    <thead>
        <tr>
            <th class="no"><?php echo $paginator->sort('No', 'id'); ?></th>
            <th><?php echo $paginator->sort('Stock Code', 'CustProductSite.stock_code'); ?></th>
            <th><?php echo $paginator->sort('Vendor Part no.', 'Product.part_no'); ?></th>
            <th><?php echo $paginator->sort('Title', 'Product.title'); ?></th>
            <th width="40px"><?php echo $paginator->sort('C/S', 'CustProductSite.current_stock'); ?></th>
            <th width="40px"><?php echo $paginator->sort('Min', 'CustProductSite.min_stock'); ?></th>
            <th width="40px"><?php echo $paginator->sort('Max', 'CustProductSite.max_stock'); ?></th>
            <th width="80px" title="Min Date is the day that current stock begin to lower than min stock.">
                    <?php echo $paginator->sort('Min Date', 'CustProductSite.min_stock_date'); ?>
            </th>
            <th width="80px" title="Max Date is the day that current stock begin to lower than max stock.">
                    <?php echo $paginator->sort('Max Date', 'CustProductSite.max_stock_date'); ?>
            </th>
            <th width="130px" title="Replenish date is the day that vendor could dispatch product to your stock.">
                    <?php echo $paginator->sort('To Replenish Date', 'CustProductSite.replenish_date'); ?>
            </th>
        </tr>
    </thead>
    <tbody>
            <?php
            $from_vendor = $session->read('Auth.User.Extend.from_supplier');
            $from_its_user = $session->read('Auth.User.Extend.from_its_user');
            $count = count($this->data)// mode edit ?>
            <?php for($i = 0; $i < $count; ++$i) : ?>
        <tr class="<?php echo $this->data[$i]['CustProductSite']['current_stock'] <= $this->data[$i]['CustProductSite']['min_stock']? 'min-stock':
                ($this->data[$i]['CustProductSite']['current_stock'] < $this->data[$i]['CustProductSite']['max_stock']? 'max-stock':
                ($this->data[$i]['CustProductSite']['current_stock'] < $this->data[$i]['CustProductSite']['max_stock']? 'max-stock':
                    ($this->data[$i]['CustProductSite']['current_stock'] >= $this->data[$i]['CustProductSite']['max_stock']? 'green': ''))); ?>">
            <td class="center"><span><?php echo $i + 1; ?></span></td>
            <td><?php echo $this->data[$i]['CustProductSite']['stock_code']; ?></td>
            <td><?php echo $this->data[$i]['CustProductSite']['dispatch_from'] == ITS_PREFIX? $this->data[$i]['Product']['part_no']: $this->data[$i]['Product']['supplier_part_no']; ?></td>
            <td><?php echo $this->data[$i]['Product']['title']; ?></td>
            <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['current_stock']; ?></td>
            <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['min_stock']; ?></td>
            <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['max_stock']; ?></td>
                    <?php if ($this->data[$i]['CustProductSite']['current_stock'] >= $this->data[$i]['CustProductSite']['max_stock']) : ?>
            <td colspan="3">
                            <?php if ($from_its_user || $from_vendor) : ?>
                <div id="divMaxStockNote<?php echo $i; ?>" class="wide"><?php echo empty($this->data[$i]['CustProductSite']['max_stock_note'])? '...': $this->data[$i]['CustProductSite']['max_stock_note']; ?></div>
                                <?php echo $ajax->editor("divMaxStockNote{$i}", "/cust_product_sites/inplace_editor/max_stock_note/{$this->data[$i]['CustProductSite']['id']}", array('highlightendcolor' => '#7FFF3F', 'formClassName' => 'wide', 'clickToEditText' => 'Click here to edit max stock reason.', 'size' => 30)); ?>
                            <?php else :?>
                <div id="divMaxStockNote<?php echo $i; ?>" class="wide"><?php echo $this->data[$i]['CustProductSite']['max_stock_note']; ?></div>
                            <?php endif; ?>
            </td>
                    <?php else : ?>
            <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['min_stock_date']; ?></td>
            <td class="text-right"><?php echo $this->data[$i]['CustProductSite']['max_stock_date']; ?></td>
            <td class="text-right">
                <span id="spanReplenishDate<?php echo $i; ?>"><?php echo $this->data[$i]['CustProductSite']['replenish_date']; ?></span>
                            <?php
                            echo $form->hidden('replenish_date', array('id' => 'replenishDate' . $i));
                            echo $html->link('Replenish date', array('controller' => 'cust_product_sites', 'action' => 'set_replenish_date', $this->data[$i]['CustProductSite']['id']), array('id' => 'linkReplenishDate' . $i, 'class' => 'hide'), null, false);
                            ?>
                        <?php endif; ?>
                        <?php if ($from_its_user || $from_vendor && ($this->data[$i]['CustProductSite']['current_stock'] < $this->data[$i]['CustProductSite']['max_stock'])) : ?>
                <script type="text/javascript">
                    $j(function() {
                        // Event การ set replenish date
                        var element = '#replenishDate<?php echo $i; ?>';
                        var span = '#spanReplenishDate<?php echo $i; ?>';
                        var link = '#linkReplenishDate<?php echo $i; ?>';
                        $j(element).datepicker($j.extend(datepicker_options, {showOn: 'button', buttonImage: base + '/img/icon/edit.png', buttonImageOnly: true,
                            onSelect: function(dateText) {
                                $j(span).update($j(link).attr('href') + '/' + dateText + '');
                            },
                            showButtonPanel: true,
                            closeText: 'Clear'
                        }));
                    });
                </script>
                        <?php endif; ?>
            </td>
        </tr>
            <?php endfor; ?>
    </tbody>
</table>
    <?php echo $this->element('paging'); ?>
<div class="caption">
    <span class="min-stock rect"></span> Current Stock is less than or equal Min Stock.
    <span class="max-stock rect"></span> Current Stock is less than Max Stock.
    <span class="green rect"></span> Current Stock is higher than Max Stock.<br />
</div>

    <?php if ($from_its_user || $from_vendor) : ?>
<script type="text/javascript">
    $j(function() {
        // เก็บ element ที่จะ clear element replenish date
        var clearRDate = {};

        $j('div[id^=divMaxStockNote]').handover();
        $j('.ui-datepicker-trigger').handover();
        $j('.ui-datepicker-trigger').bind('click', function() {
            clearRDate.link = $j(this).siblings('a').attr('href');
            clearRDate.span = '#' + $j(this).siblings('span').attr('id');
        });

        // ป่มเคลียร์บนปฏิทิน replenish_date clear ค่าวันที่
        $j('.ui-datepicker-close').live('click', function(event) {
            $j(clearRDate.span).update(clearRDate.link);
        });
    });
</script>
    <?php endif; ?>

<? endif; ?>
<?php //debug($this->data); ?>

